package com.canaan.business.controller.pc.bill;

import java.util.List;

import com.canaan.business.domain.vo.DistributeMaterialPreparationVo;
import com.canaan.business.domain.vo.MaterialPreparationExportVo;
import com.canaan.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import javax.validation.Valid;

import com.canaan.business.domain.bill.MaterialPreparation;
import com.canaan.business.service.bill.IMaterialPreparationService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 生产备料申请Controller
 *
 * @author huangxs
 * @date 2023-09-13
 */
@Api(tags = "生产备料申请")
@RestController
@RequestMapping("/bill/preparation")
public class MaterialPreparationController extends BaseController {
    @Resource
    private IMaterialPreparationService materialPreparationService;

    /**
     * 查询生产备料申请列表
     */
    @ApiOperation(value = "查询生产备料申请列表")
    @GetMapping("/list")
    public TableDataInfo list(MaterialPreparation materialPreparation) {
        startPage();
        List<MaterialPreparation> list = materialPreparationService.selectMaterialPreparationList(materialPreparation);
        return getDataTable(list);
    }

    /**
     * 导出生产备料申请列表
     */
    @ApiOperation(value = "导出生产备料申请列表")
    @Log(title = "生产备料申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialPreparation materialPreparation) {
        List<MaterialPreparation> list = materialPreparationService.selectMaterialPreparationList(materialPreparation);
        ExcelUtil<MaterialPreparation> util = new ExcelUtil<MaterialPreparation>(MaterialPreparation.class);
        util.exportExcel(response, list, "生产备料申请数据");
    }

    /**
     * 获取生产备料申请详细信息
     */
    @ApiOperation(value = "获取生产备料申请详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialPreparationService.selectMaterialPreparationById(id));
    }

    /**
     * 新增生产备料申请
     */
    @ApiOperation(value = "新增生产备料申请")
    @Log(title = "生产备料申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid MaterialPreparation materialPreparation) {
        return toAjax(materialPreparationService.insertMaterialPreparation(materialPreparation));
    }

    /**
     * 修改生产备料申请
     */
    @ApiOperation(value = "修改生产备料申请")
    @Log(title = "生产备料申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid MaterialPreparation materialPreparation) {
        return toAjax(materialPreparationService.updateMaterialPreparation(materialPreparation));
    }

    /**
     * 删除生产备料申请
     */
    @ApiOperation(value = "删除生产备料申请")
    @Log(title = "生产备料申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialPreparationService.deleteMaterialPreparationByIds(ids));
    }

    /**
     * 确认生产备料申请
     */
    @ApiOperation(value = "确认生产备料申请")
    @Log(title = "生产备料申请", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/confirm/{id}")
    public AjaxResult confirm(@PathVariable("id") Long id){
       return toAjax(materialPreparationService.confirm(id));
    }
    /**
     * 获取下发订单信息
     */
    @ApiOperation(value = "获取下发订单信息")
    @GetMapping(value = "/getDistributeInfo/{id}")
    public AjaxResult getDistributeInfo(@PathVariable("id") Long id){
        return success(materialPreparationService.getDistributeInfo(id));
    }

    /**
     * 确认下发
     */
    @ApiOperation(value = "确认下发")
    @Log(title = "确认下发", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/confirmDistribute")
    public AjaxResult confirmDistribute(@RequestBody DistributeMaterialPreparationVo vo){
       return success(materialPreparationService.confirmDistribute(vo));
    }
    /**
     * 获取库存可满足数量
     */
    @ApiOperation(value = "获取库存可满足数量")
    @GetMapping(value = "/getStockQty/{id}")
    public AjaxResult getStockQty(@PathVariable("id") Long id){
        return success(materialPreparationService.getMeetStockQty(id));
    }

    /**
     * 生产备料导入模板
     */
    @ApiOperation(value = "生产备料导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MaterialPreparationExportVo> util = new ExcelUtil<MaterialPreparationExportVo>(MaterialPreparationExportVo.class);
        util.importTemplateExcel(response, "生产备料申请数据");
    }

    /**
     * 导入生产备料申请单
     */
    @ApiOperation(value = "导入生产备料申请单")
    @PostMapping("/importExcel")
    @Log(title = "导入领料单", businessType = BusinessType.IMPORT)
    public AjaxResult importExcel(MultipartFile file) {
        List<MaterialPreparationExportVo> list;
        try {
            //读取excel文件数据
            ExcelUtil<MaterialPreparationExportVo> util = new ExcelUtil<>(MaterialPreparationExportVo.class);
            list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("导入文件解析失败");
        }
        //初步校验数据
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException("文件缺少数据");
        }

        //深入校验数据和保存数据
        List<String> errorList = materialPreparationService.importExcel(list);
        if (!errorList.isEmpty()) {
            StringBuilder failureMsg = new StringBuilder();
            failureMsg.append("很抱歉，导入失败！共 ").append(errorList.size()).append(" 条数据格式不正确，错误如下：");
            for (int i = 0; i < errorList.size(); i++) {
                failureMsg.append("<br/>").append(i + 1).append("、").append(errorList.get(i));
            }
            return error(failureMsg.toString());
        }
        return success("导入成功");
    }

}
